package com.liy.cloud.system.service.impl;

import com.liy.cloud.system.dao.SysPermissionDao;
import com.liy.cloud.system.entity.SysPermissionEntity;
import com.liy.cloud.system.service.SysPermissionService;
import com.liy.framework.common.dao.IBaseDao;
import com.liy.framework.common.utils.ContextHandler;
import com.liy.framework.common.utils.DateUtil;
import com.liy.framework.common.utils.Tools;
import com.liy.framework.web.spring.service.impl.BaseServiceImpl;
import java.util.Iterator;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SysPermissionServiceImpl extends BaseServiceImpl<SysPermissionEntity, String> implements SysPermissionService {

    @Autowired
    private SysPermissionDao sysPermissionDao;

    public SysPermissionServiceImpl() {
    }

    public IBaseDao<SysPermissionEntity, String> getDao() {
        return this.sysPermissionDao;
    }

    public void savePermissionList(List<SysPermissionEntity> list) {
        SysPermissionEntity temp = null;
        String version = DateUtil.getFormatDate("yyMMddHHmmss");
        String sysId = null;
        Iterator var5 = list.iterator();

        while(var5.hasNext()) {
            SysPermissionEntity sysPermissionEntity = (SysPermissionEntity)var5.next();
            sysId = sysPermissionEntity.getSysId();
            sysPermissionEntity.setVersion(version);
            temp = this.sysPermissionDao.selectBySysIdAndUrl(sysPermissionEntity.getSysId(), sysPermissionEntity.getUrl());
            if (temp != null) {
                sysPermissionEntity.setId(temp.getId());
                sysPermissionEntity.setLastUpdateBy(ContextHandler.getLoginUser().getId());
                sysPermissionEntity.setLastUpdateDate(DateUtil.getTimeNow());
                this.sysPermissionDao.updateByPrimaryKeySelective(sysPermissionEntity);
            } else {
                sysPermissionEntity.setId(Tools.getNewUUID());
                sysPermissionEntity.setCreateBy(ContextHandler.getLoginUser().getId());
                sysPermissionEntity.setCreateDate(DateUtil.getTimeNow());
                this.sysPermissionDao.insertSelective(sysPermissionEntity);
            }
        }

        this.sysPermissionDao.deleteInvalidPermission(sysId, version);
    }

    public List<SysPermissionEntity> findAll() {
        return this.sysPermissionDao.findAll();
    }
}

